package com.ds.topmenu.component;

import com.ds.common.JDSException;
import com.ds.config.ErrorListResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.dsm.repository.entity.EntityTree;
import com.ds.enums.db.MethodChinaName;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.annotation.PopTreeViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.repository.RepositoryInst;
import com.ds.esd.tool.ui.enums.Dock;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@Controller
@RequestMapping(value = {"/action/tool/component/"})
@MethodChinaName(cname = "工具")
public class ComponentMenu {

    @RequestMapping(value = {"RepositoryTree"}, method = {RequestMethod.POST})
    @PopTreeViewAnnotation
    @DialogAnnotation
    @ModuleAnnotation(width = "400", caption = "绑定实体", dynLoad = true, dock = Dock.fill, imageClass = "fa fa-calendar-plus-o")
    @APIEventAnnotation(isAllform = true, autoRun = true)
    @ResponseBody
    public TreeListResultModel<List<EntityTree>> getESDTreeView(String repositoryId) {
        TreeListResultModel<List<EntityTree>> result = new TreeListResultModel<List<EntityTree>>();
        try {
            RepositoryInst bean = DSMFactory.getInstance().getRepositoryManager().getRepositoryInstById(repositoryId);
            List<EntityTree> treeViews = new ArrayList<>();
            treeViews.add(new EntityTree(repositoryId));
            result.setData(treeViews);
            result.setIds(Arrays.asList(bean.getEntityNames().toArray(new String[]{})));
        } catch (JDSException e) {
            result = new ErrorListResultModel<>();
            ((ErrorListResultModel) result).setErrcode(e.getErrorCode());
            ((ErrorListResultModel) result).setErrdes(e.getMessage());
        }
        return result;
    }
}
